//app.js
App({
    onLaunch: function () {
        
    },


    wxLogin:function(){
      return new Promise((resolve, reject) => {
        wx.login({
          success: res => {
            resolve(res.code)
          },
          fail: err => {
            reject(err)
          }
        })
      })
    },

    getUserInfo:function(){
      
      return new Promise((resolve,reject) =>{
        var userinfo = wx.getStorageSync('userDo');
        if (!userinfo) {
          this.wxLogin().then(res => {
            return this.getOpenId(res)
          })
            .then(res => {
              if (res.data.code == 0) {
                wx.setStorageSync('kyboppenid', res.data.openid)
                return this.getUsersMsg(res.data.openid)
              }
            })
            .then(res => {
              if (res.data.code == 0) {
                resolve(res.data.userDO)
                wx.setStorageSync('userDo', res.data.userDO)
              }else{
                resolve(0)
               
              }
            })
        }
      })
     
    },

    getOpenId:function(c){
      return new Promise((resolve,reject) =>{
        wx.request({
          url: this.globalData.api +'getOpenId',
          data:{code:c},
          success:res =>{
            resolve(res)
          },
          fail: err => {
            reject(err)
          }
        })
      })
    },

    getUsersMsg:function(id){
      return new Promise((resolve,reject) => {
        wx.request({
          url: this.globalData.api + 'getUsersMsg',
          data: { appOpenid:id},
          success:res=>{
            resolve(res)
           
          },
          fail: err => {
            reject(err)
          }
        })
      })
    },
    globalData: {
        userInfo: null,
        api: 'http://192.168.0.107/api/agentManage/',
        // api: 'https://takeaway.kayuanwang.cn/api/agentManage/'

    }
})